package com.netty.resolver;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.netty.anno.Mapping;
import com.netty.anno.Service;
import com.netty.collection.EntityContainer;
import com.netty.entity.MethodType;

public class AnnoScanResolver {
	String packageName;

	@Test
	public void test() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		// findService("com.netty.service");
		Class clz = Class.forName("com.netty.anno.Service");
		// Object obj = clz.newInstance();
		// Method[] methods = obj.getClass().getMethods();
		// for (int j = 0; j < methods.length; j++) {
		// System.out.println(methods[j]);
		// System.out.println(methods[j].getName());
		// for (int i = 0; i < methods[j].getParameterTypes().length; i++) {
		// System.out.print(methods[j].getParameterTypes()[i].getName() + " ");
		//
		// }
		// System.out.println(" ");
		// for (int i = 0; i < methods[j].getParameters().length; i++) {
		// System.out.print(methods[j].getParameters()[i].getName() + " ");
		//
		// }
		// for (int i = 0; i < methods[j].getParameters().length; i++) {
		// System.out.print(methods[j].getParameters()[i] + " ");
		//
		// }
		// System.out.println(" ");
		// }
	}

	public static void findService(String packageName)
			throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		List<String> classes = ResourceResolver.getResourceFile(packageName);
		for (int i = 0; i < classes.size(); i++) {
			Class clz = Class.forName(classes.get(i));
			if (clz.isInterface() || clz.isAnnotation() || clz.isEnum())
				continue;
			if (clz.isLocalClass() || clz.isMemberClass() || clz.isAnonymousClass())
				continue;
			Object obj = null;
			try {
				obj = clz.newInstance();
			} catch (Exception e) {
				continue;
			}
			Service service = obj.getClass().getAnnotation(Service.class);
			if (service == null)
				continue;
			EntityContainer.getInstance().add(service.value(), obj);
			Method[] methods = obj.getClass().getMethods();
			Map<String, MethodType> methodsMap = new HashMap<String, MethodType>();
			for (int j = 0; j < methods.length; j++) {
				Mapping maps = null;
				if ((maps = methods[j].getAnnotation(Mapping.class)) != null) {
					System.out.println(maps);
					String methodValue = maps.value();

					if ("".equals(methodValue)) {
						methodValue = "/" + methods[j].getName();
					}
					String dataType = maps.dataType();
					if ("".equals(dataType)) {
						dataType = "text/html";
					}
					MethodType methodType = new MethodType();
					methodType.setDataType(dataType);
					methodType.setMethod(methods[j]);
					methodsMap.put(methodValue, methodType);
				}
			}
			EntityContainer.getInstance().addField(service.value(), methodsMap);

		}
	}

	public static Boolean checkAnno(Annotation[] annotations) {
		if (annotations == null || annotations.length < 1)
			return false;
		return true;
	}
}
